package BeenChina.PM.Action;

 
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat; 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import net.sf.json.JSONArray;

 
import BeenChina.PM.Model.ModelCaseCostCaseInfo;
import BeenChina.PM.Model.ModelCaseCostCostInfo; 
import BeenChina.PM.Model.ModelCaseInfo; 
import BeenChina.PM.Model.ModelUser;
import BeenChina.PM.Service.ServiceFactory;
import BeenChina.PM.Util.EncodeDownLoadFileName;
import BeenChina.PM.Util.ExportInfo;
import BeenChina.PM.Util.ExportToExcel;
import BeenChina.PM.Util.StringUtil; 
import BeenChina.PM.Util.xmlSerializer;
 
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class CaseCostDetailQueryAction extends ActionSupport {

 
	private static final long serialVersionUID = 1L;
	private String dateFrom,dateTo,currency;
	private Map<String, Object> tableData;  
	private String[] cases;	
	private List<ModelCaseInfo> caseList = new ArrayList<ModelCaseInfo>();
	private InputStream excelStream;  
	private String fileName;
	private static String EXPORTID = "481";
	private boolean exportExcel; 
	private static final Log log = LogFactory.getLog(CaseCostDetailQueryAction.class);

	public String getCurrencyUnit() {
		return currency.equals("rmb")? "元":"円";
	}
	
	public String getCurrency() {
		return currency.equals("rmb")? "人民元":"日本円";
	}

	public void setCurrency(String currency) {
		this.currency = currency;
	}
	
	public boolean isExportExcel() {
		return exportExcel;
	} 
	
	public InputStream getExcelStream() {
		return excelStream;
	}

	public void setExcelStream(InputStream excelStream) {
		this.excelStream = excelStream;
	}

	public String getFileName() throws UnsupportedEncodingException {		
		ServletActionContext.getResponse().setHeader("charset","UTF-8");
		fileName = EncodeDownLoadFileName.encode(ServletActionContext.getRequest(), fileName); 
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	
	public void setDateFrom(String dateFrom) {
		this.dateFrom = dateFrom;
	} 

	public Map<String, Object> getTableData() {
		return tableData;
	}
  
	public void setDateTo(String dateTo) {
		this.dateTo = dateTo;
	} 
	public void setCases(String[] cases){
		this.cases = cases;
	}
	public List<ModelCaseInfo> getCaseList() {
		return caseList;
	}
	
	@Override
	public String execute()	{	
		ModelUser user = (ModelUser) ActionContext.getContext().getSession().get("user");
		if (user!=null)
		  exportExcel = user.concatPermission(EXPORTID);
		
		caseList = ServiceFactory.getInstance().getCommonInfoService().selectCaseNames();
  		return "load";
	}
	
	
	public List<ModelCaseCostCaseInfo> getCaseInfo()	throws Exception {
		 
		String caseStr=StringUtils.join(cases,",");
		 
		try{
			 if (StringUtil.isDate(dateFrom,"yyyy-MM") && 
				 StringUtil.isDate(dateTo,"yyyy-MM")  &&
				 caseStr.length()>0){
				return   ServiceFactory.getInstance().getCaseProductionInfoService().selectCaseCostCaseInfo(dateFrom, dateTo,caseStr,getCurrency())	;			 				
			 }
			 else {
				 return null;
			}
		 }
		 catch(Exception exp){
			 throw exp;
		 }
	}
	
	public List<ModelCaseCostCostInfo> getCostInfo()	throws Exception {
		String caseStr=StringUtils.join(cases,",");
		try{
			 if (StringUtil.isDate(dateFrom,"yyyy-MM") && 
					 StringUtil.isDate(dateTo,"yyyy-MM") &&
					 caseStr.length()>0) {
				return   ServiceFactory.getInstance().getCaseProductionInfoService().selectCaseCostCostInfo(dateFrom, dateTo,caseStr,getCurrency())	;			 				
			 }
			 else {
				 return null;
			}
		 }
		 catch(Exception exp){
			 throw exp;
		 }
	}
	
	 public String refreshData() throws Exception
	 {  
		 
		 List<ModelCaseCostCaseInfo> caseInfos =  getCaseInfo();
		 List<ModelCaseCostCostInfo> costInfos = getCostInfo();
		  
		 Map<String, Object> map = new HashMap<String, Object>();   
		 
		 map.put("CaseInfoTable", getCaseInfoTable(caseInfos)); 
		 map.put("CostInfoTableA", getCostInfoTableA(costInfos));
		 map.put("CostInfoTableB", getCostInfoTableB(costInfos));
		 
		 Map<String, Object> chartA = getCostInfoChartA(costInfos);	  
	
		 map.put("OPEntryCost", chartA.get("OPEntryCost"));
		 map.put("ServersCost", chartA.get("ServersCost"));
		 map.put("WelfareCost", chartA.get("WelfareCost"));
		 map.put("OtherCost", chartA.get("OtherCost"));
		 map.put("TotalCost", chartA.get("TotalCost"));
		 
		 Map<String, Object> chartB = getCostInfoChartB(costInfos);	
		 map.put("EveryOPEntryCost",chartB.get("EveryOPEntryCost "));
		 map.put("EveryServersCost",chartB.get("EveryServersCost"));
		 map.put("EveryInsuranceCost",chartB.get("EveryInsuranceCost"));
		 map.put("EveryAllowanceCost",chartB.get("EveryAllowanceCost"));
		 map.put("EveryOtherCost1",chartB.get("EveryOtherCost1"));
		 map.put("EveryOtherCost2",chartB.get("EveryOtherCost2"));
		 map.put("EveryTotalCost",chartB.get("EveryTotalCost"));
		 
		 map.put("EveryOPEntryCostB",chartB.get("EveryOPEntryCostB"));
		 map.put("EveryServersCostB",chartB.get("EveryServersCostB"));
		 map.put("EveryInsuranceCostB",chartB.get("EveryInsuranceCostB"));
		 map.put("EveryAllowanceCostB",chartB.get("EveryAllowanceCostB"));
		 map.put("EveryOtherCost1B",chartB.get("EveryOtherCost1B"));
		 map.put("EveryOtherCost2B",chartB.get("EveryOtherCost2B"));
		 map.put("EveryTotalCostB",chartB.get("EveryTotalCostB"));
		 
		 tableData = map;  
	     return ActionSupport.SUCCESS; 
	 }

	 private Map<String, Object> getCostInfoChartB(List<ModelCaseCostCostInfo> list) {
			Map<String, Object> map = new HashMap<String, Object>();   
			 //chart
			 JSONArray mapEveryOPEntryCost = new JSONArray(),mapEveryServersCost = new JSONArray(),
					   mapEveryInsuranceCost = new JSONArray(),mapEveryAllowanceCost = new JSONArray(),
					   mapEveryOtherCost1 = new JSONArray(),mapEveryOtherCost2 = new JSONArray(),
					   mapEveryTotalCost = new JSONArray();
			 
			 JSONArray mapEveryOPEntryCostB = new JSONArray(),mapEveryServersCostB = new JSONArray(),
					   mapEveryInsuranceCostB = new JSONArray(),mapEveryAllowanceCostB = new JSONArray(),
					   mapEveryOtherCost1B = new JSONArray(),mapEveryOtherCost2B = new JSONArray(),
					   mapEveryTotalCostB = new JSONArray();
			 
			 DecimalFormat df = new DecimalFormat("#.0"); 
			 if (list !=null && list.size()>0)
			 {
				for (ModelCaseCostCostInfo model : list) {
					
					
					JSONArray json = new JSONArray();
					//json.add(model.getDate());
					json.add(df.format(model.getEveryOpEntryCost()));
					mapEveryOPEntryCost.add(json);
					
					Double tDouble = model.getEveryOpEntryCost();
					json = new JSONArray();
					json.add(model.getDate());
					json.add(df.format(tDouble));
					mapEveryOPEntryCostB.add(json);
						
					
					json = new JSONArray();
					//json.add(model.getDate());
					json.add(df.format(model.getEveryServersCost()));
					mapEveryServersCost.add(json);
					
					tDouble += model.getEveryServersCost();
					json = new JSONArray();
					json.add(model.getDate());
					json.add(df.format(tDouble));
					mapEveryServersCostB.add(json);
					
					json = new JSONArray();
					//json.add(model.getDate());
					json.add(df.format(model.getEveryInsuranceCost())); 
					mapEveryInsuranceCost.add(json);
					
					tDouble += model.getEveryInsuranceCost();
					json = new JSONArray();
					json.add(model.getDate());
					json.add(df.format(tDouble));
					mapEveryInsuranceCostB.add(json);
					
					json = new JSONArray();
					//json.add(model.getDate());
					json.add(df.format(model.getEveryAllowanceCost()));					
					mapEveryAllowanceCost.add(json);
					
					tDouble += model.getEveryAllowanceCost();
					json = new JSONArray();
					json.add(model.getDate());
					json.add(df.format(tDouble));
					mapEveryAllowanceCostB.add(json);
					
					json = new JSONArray();
					//json.add(model.getDate());
					json.add(df.format(model.getEveryOtherCost1()));
					mapEveryOtherCost1.add(json);
					
					tDouble += model.getEveryOtherCost1();
					json = new JSONArray();
					json.add(model.getDate());
					json.add(df.format(tDouble));
					mapEveryOtherCost1B.add(json);
					
					json = new JSONArray();
					//json.add(model.getDate());
					json.add(df.format(model.getEveryOtherCost2()));					
					mapEveryOtherCost2.add(json);
					
					tDouble += model.getEveryOtherCost2();
					json = new JSONArray();
					json.add(model.getDate());
					json.add(df.format(tDouble));
					mapEveryOtherCost2B.add(json);
					
					json = new JSONArray();
					json.add(model.getDate());
					json.add(df.format(model.getEveryTotalCost()));
					 mapEveryTotalCost.add(json);
					 
					 
				}
			 }
			 
			map.put("EveryOPEntryCost ",mapEveryOPEntryCost);
			map.put("EveryServersCost",mapEveryServersCost);
			map.put("EveryInsuranceCost",mapEveryInsuranceCost);
			map.put("EveryAllowanceCost",mapEveryAllowanceCost);
			map.put("EveryOtherCost1",mapEveryOtherCost1 );
			map.put("EveryOtherCost2",mapEveryOtherCost2);
			map.put("EveryTotalCost",mapEveryTotalCost);
		
			map.put("EveryOPEntryCostB",mapEveryOPEntryCostB);
			map.put("EveryServersCostB",mapEveryServersCostB);
			map.put("EveryInsuranceCostB",mapEveryInsuranceCostB);
			map.put("EveryAllowanceCostB",mapEveryAllowanceCostB);
			map.put("EveryOtherCost1B",mapEveryOtherCost1B );
			map.put("EveryOtherCost2B",mapEveryOtherCost2B);
			map.put("EveryTotalCostB",mapEveryTotalCostB);
			 
			
			 return map;
		}
	 
	private Map<String, Object> getCostInfoChartA(List<ModelCaseCostCostInfo> list) {
		Map<String, Object> map = new HashMap<String, Object>();   
		 //chart
		 JSONArray mapOPEntryCost = new JSONArray(),mapServersCost = new JSONArray(),
				   mapWelfareCost = new JSONArray(),mapOtherCost = new JSONArray(),
				   mapTotalCost = new JSONArray();
		 DecimalFormat df = new DecimalFormat("#.0"); 
		 if (list !=null && list.size()>0)
		 {
			for (ModelCaseCostCostInfo model : list) {
				
				JSONArray json = new JSONArray();
				json.add(model.getDate());
				json.add(df.format(model.getOpEntryCost()));
				mapOPEntryCost.add(json);
				
				json = new JSONArray();
				json.add(model.getDate());
				json.add(df.format(model.getServersCost()));
				mapServersCost.add(json);
				
				
				json = new JSONArray();
				json.add(model.getDate());
				json.add(df.format(model.getWelfareCost()));
				mapWelfareCost.add(json);
				
				json = new JSONArray();
				json.add(model.getDate());
				json.add(df.format(model.getOtherCost()));
				mapOtherCost.add(json);
				
				json = new JSONArray();
				json.add(model.getDate());
				json.add(df.format(model.getTotalCost()));
				mapTotalCost.add(json);
			}
		 }
		 
		 map.put("OPEntryCost", mapOPEntryCost);
		 map.put("ServersCost", mapServersCost);
		 map.put("WelfareCost", mapWelfareCost);
		 map.put("OtherCost", mapOtherCost);
		 map.put("TotalCost", mapTotalCost);
		 
		 return map;
	}
	
	private JSONArray getCostInfoTableB(List<ModelCaseCostCostInfo> list) {
		JSONArray json = new JSONArray();	
		JSONArray map = new JSONArray(); 
		 if (list ==null || list.size() ==0)
		 {
			 return map;
		 }		 
		 
		 DecimalFormat df = new DecimalFormat("#.0"); 
		 Map<String, JSONArray> detail = new  HashMap<String, JSONArray>();			
	
		 json = new JSONArray();json.add("人員");detail.put("EveryOPEntryCost",json);
		 json = new JSONArray();json.add("設備");detail.put("EveryServersCost",json);	
		 json = new JSONArray();json.add("福利厚生");detail.put("EveryInsuranceCost",json);
		 json = new JSONArray();json.add("福利手当て");detail.put("EveryAllowanceCost",json);
		 json = new JSONArray();json.add("その他1");detail.put("EveryOtherCost1",json);
		 json = new JSONArray();json.add("その他2");detail.put("EveryOtherCost2",json);
		 json = new JSONArray();json.add("合計");detail.put("EveryTotalCost",json);	

		 JSONArray jsonTittle = new JSONArray();		 
		 jsonTittle.add("1枚当りコスト(" + getCurrencyUnit() +")");
		 
		 
		 for (int i = 0; i < list.size(); i++) {
			 ModelCaseCostCostInfo model = list.get(i); 
		     
			  
			 jsonTittle.add(model.getDate());
			 detail.get("EveryOPEntryCost").add(df.format(model.getEveryOpEntryCost()));
			 detail.get("EveryServersCost").add(df.format(model.getEveryServersCost()));
			 detail.get("EveryInsuranceCost").add(df.format(model.getEveryInsuranceCost()));
			 detail.get("EveryAllowanceCost").add(df.format(model.getEveryAllowanceCost()));
			 detail.get("EveryOtherCost1").add(df.format(model.getEveryOtherCost1()));
			 detail.get("EveryOtherCost2").add(df.format(model.getEveryOtherCost2()));
			 detail.get("EveryTotalCost").add(df.format(model.getEveryTotalCost()));
		 }
		 map.add(jsonTittle);
		 map.add( detail.get("EveryOPEntryCost")); 
		 map.add( detail.get("EveryServersCost") );
		 map.add( detail.get("EveryInsuranceCost") );
		 map.add( detail.get("EveryAllowanceCost") );
		 map.add( detail.get("EveryOtherCost1") );
		 map.add( detail.get("EveryOtherCost2") );
		 map.add( detail.get("EveryTotalCost") );
		 
		 return map;
	}

	private JSONArray getCostInfoTableA(List<ModelCaseCostCostInfo> list) {
		JSONArray json = new JSONArray();	
		JSONArray map = new JSONArray(); 
		 if (list ==null || list.size() ==0)
		 {
			 return map;
		 }		 
		 
		 DecimalFormat df = new DecimalFormat("#.0"); 
		 Map<String, JSONArray> detail = new  HashMap<String, JSONArray>();			
	
		 json = new JSONArray();json.add("人員");detail.put("OPEntryCost",json);
		 json = new JSONArray();json.add("設備");detail.put("ServersCost",json);	
		 json = new JSONArray();json.add("福利");detail.put("WelfareCost",json);	
		 json = new JSONArray();json.add("その他");detail.put("OtherCost",json);
		 json = new JSONArray();json.add("合計");detail.put("TotalCost",json);	

		 JSONArray jsonTittle = new JSONArray();		 
		 jsonTittle.add("月総コスト(万" + getCurrencyUnit() +")");
		 
		 
		 for (int i = 0; i < list.size(); i++) {
			 ModelCaseCostCostInfo model = list.get(i); 
		     
			  
			 jsonTittle.add(model.getDate());
			 detail.get("OPEntryCost").add(df.format(model.getOpEntryCost()));
			 detail.get("ServersCost").add(df.format(model.getServersCost()));
			 detail.get("WelfareCost").add(df.format(model.getWelfareCost()));
			 detail.get("OtherCost").add(df.format(model.getOtherCost()));
			 detail.get("TotalCost").add(df.format(model.getTotalCost()));
		 }
		 
		 map.add(jsonTittle);
		 map.add( detail.get("OPEntryCost")); 
		 map.add( detail.get("ServersCost") );
		 map.add( detail.get("WelfareCost") );
		 map.add( detail.get("OtherCost") );
		 map.add( detail.get("TotalCost") );
		 
		 return map;
	}

	private JSONArray getCaseInfoTable(
			List<ModelCaseCostCaseInfo> list) {
		
		JSONArray json = new JSONArray();	
		JSONArray map = new JSONArray(); 
		 if (list ==null || list.size() ==0)
		 {
			 return map;
		 }
		 
		 
		 DecimalFormat df = new DecimalFormat("#.0"); 
		 Map<String, JSONArray> detail = new  HashMap<String, JSONArray>();
	
		 json = new JSONArray();  json.add("1"); json.add("入力情報");  json.add("納品量");  json.add("枚");  detail.put("ReceiptCount",json); 
		 json = new JSONArray();  json.add("2"); json.add("");  json.add("案件毎総処理時間");  json.add("時間");  detail.put("EntryTime",json); 
		 json = new JSONArray();  json.add("3"); json.add("");  json.add("案件毎処理時間比率");  json.add("%");  detail.put("CaseEntryRate",json); 
		 json = new JSONArray();  json.add("4"); json.add("生産性");  json.add("1枚当たり生産性");  json.add("秒/枚");  detail.put("AVGEntryTime",json); 
		 json = new JSONArray();  json.add("5"); json.add("人員コスト");  json.add("オペレータ入力コスト");  json.add("万" + getCurrencyUnit() );  detail.put("OPEntryCost",json); 
		 json = new JSONArray();  json.add("6"); json.add("");  json.add("1枚当たりコスト");  json.add(  getCurrencyUnit() +"/枚");  detail.put("EveryCost",json); 
		 json = new JSONArray();  json.add("7"); json.add("設備コスト");  json.add("サーバコスト");  json.add("万" + getCurrencyUnit() );  detail.put("ServersCost",json); 
		 json = new JSONArray();  json.add("8"); json.add("福利コスト");  json.add("福利厚生コスト");  json.add("万" + getCurrencyUnit() );  detail.put("InsuranceCost",json); 
		 json = new JSONArray();  json.add("9"); json.add("");  json.add("1枚当たりコスト");  json.add(  getCurrencyUnit() + "/枚");  detail.put("EveryInsuranceCost",json); 
		 json = new JSONArray();  json.add("10"); json.add("");  json.add("福利手当てコスト");  json.add("万" + getCurrencyUnit() );  detail.put("AllowanceCost",json); 
		 json = new JSONArray();  json.add("11"); json.add("");  json.add("1枚当たりコスト");  json.add(  getCurrencyUnit() +"/枚");  detail.put("EveryAllowanceCost",json); 
		 json = new JSONArray();  json.add("12"); json.add("その他コスト");  json.add("間接価格コスト");  json.add("万" + getCurrencyUnit() );  detail.put("OtherCost1",json); 
		 json = new JSONArray();  json.add("13"); json.add("");  json.add("1枚当たりコスト");  json.add( getCurrencyUnit() + "/枚");  detail.put("EveryOtherCost1",json); 
		 json = new JSONArray();  json.add("14"); json.add("");  json.add("割り掛けコスト");  json.add("万" + getCurrencyUnit() );  detail.put("OtherCost2",json); 
		 json = new JSONArray();  json.add("15"); json.add("");  json.add("1枚当たりコスト");  json.add( getCurrencyUnit()  + "/枚");  detail.put("EveryOtherCost2",json); 
		 json = new JSONArray();  json.add("16"); json.add("実際直接コスト1");  json.add("総コスト");  json.add("万" + getCurrencyUnit() );  detail.put("RealityCost1",json); 
		 json = new JSONArray();  json.add("17"); json.add("");  json.add("1枚当たりコスト");  json.add(  getCurrencyUnit() +"/枚");  detail.put("RealityEveryCost1",json); 
		 json = new JSONArray();  json.add("18"); json.add("総分担コスト1");  json.add("総コスト");  json.add("万" + getCurrencyUnit());  detail.put("ShareCost1",json); 
		 json = new JSONArray();  json.add("19"); json.add("");  json.add("1枚当たりコスト");  json.add(  getCurrencyUnit() +"/枚");  detail.put("ShareEveryCost1",json); 
		 json = new JSONArray();  json.add("20"); json.add("実際直接コスト2");  json.add("総コスト");  json.add("万" + getCurrencyUnit());  detail.put("RealityCost2",json); 
		 json = new JSONArray();  json.add("21"); json.add("");  json.add("1枚当たりコスト");  json.add(  getCurrencyUnit()+"/枚");  detail.put("RealityEveryCost2",json); 
		 json = new JSONArray();  json.add("22"); json.add("総分担コスト2");  json.add("総コスト");  json.add("万" + getCurrencyUnit());  detail.put("ShareCost2",json); 
		 json = new JSONArray();  json.add("23"); json.add("");  json.add("1枚当たりコスト");  json.add(   getCurrencyUnit() + "/枚");  detail.put("ShareEveryCost2",json); 
		 json = new JSONArray();  json.add("24"); json.add("実際直接コスト3");  json.add("総コスト");  json.add("万" + getCurrencyUnit());  detail.put("RealityCost3",json); 
		 json = new JSONArray();  json.add("25"); json.add("");  json.add("1枚当たりコスト");  json.add(   getCurrencyUnit() + "/枚");  detail.put("RealityEveryCost3",json); 
		 json = new JSONArray();  json.add("26"); json.add("総分担コスト3");  json.add("総コスト");  json.add("万"  + getCurrencyUnit());  detail.put("ShareCost3",json); 
		 json = new JSONArray();  json.add("27"); json.add("");  json.add("1枚当たりコスト");  json.add(  getCurrencyUnit() + "/枚");  detail.put("ShareEveryCost3 ",json); 
		 
		 JSONArray jsonTittle = new JSONArray();
		 jsonTittle.add("No.");
		 jsonTittle.add("類別");
		 jsonTittle.add("類別詳細"); 
		 jsonTittle.add("単位");
		 
		 for (int i = 0; i < list.size(); i++) {
			 ModelCaseCostCaseInfo model = list.get(i); 
		     
			 //add tittle
			 jsonTittle.add(model.getDate());
			 detail.get("ReceiptCount").add(model.getReceiptCount());
			 detail.get("EntryTime").add(df.format(model.getEntryTime()));
			 detail.get("CaseEntryRate").add(model.getCaseEntryRate());
			 detail.get("AVGEntryTime").add(df.format(model.getAvgEntryTime()));
			 detail.get("OPEntryCost").add(df.format(model.getOpEntryCost()));
			 detail.get("EveryCost").add(df.format(model.getEveryCost()));
			 detail.get("ServersCost").add(df.format(model.getServersCost()));
			 detail.get("InsuranceCost").add(df.format(model.getInsuranceCost()));
			 detail.get("EveryInsuranceCost").add(df.format(model.getEveryInsuranceCost()));
			 detail.get("AllowanceCost").add(df.format(model.getAllowanceCost()));
			 detail.get("EveryAllowanceCost").add(df.format(model.getEveryAllowanceCost()));
			 detail.get("OtherCost1").add(df.format(model.getOtherCost1()));
			 detail.get("EveryOtherCost1").add(df.format(model.getEveryOtherCost1()));
			 detail.get("OtherCost2").add(df.format(model.getOtherCost2()));
			 detail.get("EveryOtherCost2").add(df.format(model.getEveryOtherCost2()));
			 detail.get("RealityCost1").add(df.format(model.getRealityCost1()));
			 detail.get("RealityEveryCost1").add(df.format(model.getRealityEveryCost1()));
			 detail.get("ShareCost1").add(df.format(model.getShareCost1()));
			 detail.get("ShareEveryCost1").add(df.format(model.getShareEveryCost1()));
			 detail.get("RealityCost2").add(df.format(model.getRealityCost2()));
			 detail.get("RealityEveryCost2").add(df.format(model.getRealityEveryCost2()));
			 detail.get("ShareCost2").add(df.format(model.getShareCost2()));
			 detail.get("ShareEveryCost2").add(df.format(model.getShareEveryCost2()));
			 detail.get("RealityCost3").add(df.format(model.getRealityCost3()));
			 detail.get("RealityEveryCost3").add(df.format(model.getRealityEveryCost3()));
			 detail.get("ShareCost3").add(df.format(model.getShareCost3()));
			 detail.get("ShareEveryCost3 ").add(df.format(model.getShareEveryCost3()));
		 }
		map.add(jsonTittle);
		map.add(detail.get("ReceiptCount"));
		map.add(detail.get("EntryTime"));
		map.add(detail.get("CaseEntryRate"));
		map.add(detail.get("AVGEntryTime"));
		map.add(detail.get("OPEntryCost"));
		map.add(detail.get("EveryCost"));
		map.add(detail.get("ServersCost"));
		map.add(detail.get("InsuranceCost"));
		map.add(detail.get("EveryInsuranceCost"));
		map.add(detail.get("AllowanceCost"));
		map.add(detail.get("EveryAllowanceCost"));
		map.add(detail.get("OtherCost1"));
		map.add(detail.get("EveryOtherCost1"));
		map.add(detail.get("OtherCost2"));
		map.add(detail.get("EveryOtherCost2"));
		map.add(detail.get("RealityCost1"));
		map.add(detail.get("RealityEveryCost1"));
		map.add(detail.get("ShareCost1"));
		map.add(detail.get("ShareEveryCost1"));
		map.add(detail.get("RealityCost2"));
		map.add(detail.get("RealityEveryCost2"));
		map.add(detail.get("ShareCost2"));
		map.add(detail.get("ShareEveryCost2"));
		map.add(detail.get("RealityCost3"));
		map.add(detail.get("RealityEveryCost3"));
		map.add(detail.get("ShareCost3"));
		map.add(detail.get("ShareEveryCost3 "));
			 
		 return map;
	}

 
	public String export() throws Exception{		 
		try{	
			String file;
			
			if (this.currency.equals("rmb")){
			
				file = ServletActionContext.getServletContext().getRealPath("WEB-INF/excel/CaseCostDetailQuery_RMB.xls");
			}
			else{
				file = ServletActionContext.getServletContext().getRealPath("WEB-INF/excel/CaseCostDetailQuery_YEN.xls");
			}
				
			 List<ModelCaseCostCaseInfo> caseInfos =  getCaseInfo();
			 List<ModelCaseCostCostInfo> costInfos = getCostInfo(); 
			
			 
			Map<String, JSONArray> map = new HashMap<String, JSONArray>();   		 
			 map.put("案件運用関連報告(瀋陽部分のみ)", getCaseInfoTable(caseInfos)); 
			 map.put("案件別1枚当り費用_構成", getCostInfoTableB(costInfos));
			 map.put("案件別月総費用_構成", getCostInfoTableA(costInfos)); 
							
			
			 JSONArray jsonTittleArray = new JSONArray();
			 String caseStr=StringUtils.join(cases,","); 
			 jsonTittleArray.add("■案件コスト状況・詳細_【" + caseStr + "】"); 
			 JSONArray json = new JSONArray();
			 json.add(jsonTittleArray);
			 map.put("案件コスト状況", json );
			 
			 
			 
			
			ByteArrayOutputStream out = new ByteArrayOutputStream();			
			this.setFileName("案件コスト状況・詳細「" +getCurrency()+"」_"+"【"+ caseStr + "】"+"_"+ dateFrom +"~" + dateTo );
			
			String infoFile=ServletActionContext.getServletContext().getRealPath("WEB-INF/excel/CaseCostDetailQuery.xml");
			List<ExportInfo> infoList = (new xmlSerializer<List<ExportInfo>>()).read(infoFile);
			
			ExportToExcel.export(file, map, infoList,out);
			
			setExcelStream(new ByteArrayInputStream(out.toByteArray()));	   			   			      
			out.close();
		
			return ActionSupport.SUCCESS;
		}
		catch(Exception exp){
			log.error(exp);
			return ActionSupport.ERROR;
		}		
	}			
	
}
